home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 5 / The 640 Meg Shareware Studio CD-ROM Volume V (Data Express)(1994).ISO / amiga / tempdemo.lha / ProgrammersInfo / InternodeLineChat.mod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-22  |  13.4 KB  |  527 lines

  1. /*
  2.  
  3.      .----------------------------------------------------------------.
  4.      | NODE | LOGON | HANDLE                    | ACTiViTY            |
  5.      |======|=======|===========================|=====================|
  6.      |   1  | 17:45 | The Skeleton              | Chat System         |
  7.      |------|-------|---------------------------|---------------------|
  8.      |   2  | --:-- | Awaiting a call           | Idle                |
  9.      |------|-------|---------------------------|---------------------|
  10.      |   3  | --:-- | Awaiting a call           | Idle                |
  11.      `----------------------------------------------------------------'
  12.  
  13. Press <CTRL-?> to see menu.
  14.  
  15.  
  16. <CTRL-P> Page node for chat
  17. <CTRL-S> Send private text
  18. <CTRL-W> Who's Online
  19. <CTRL-X> Exit Chat
  20.  
  21.  
  22. */
  23. //**********************
  24. //*****  Includes  *****
  25. //**********************
  26.  
  27. #include <proto/all.h>
  28. #include <stdio.h>
  29.  
  30. #include <string.h>
  31. #include <tempest/headers.h>
  32.  
  33. //********************************
  34. //*****  Structures/Defines  *****
  35. //********************************
  36.  
  37. struct MyMessage
  38.  {
  39.   struct Message Msg;
  40.   struct User User;
  41.   char text[255],
  42.        text1[255];
  43.   int car,Value,Data;
  44.   long LongValue;
  45.   int  IntValue;
  46.  };
  47.  
  48. struct User User;
  49. struct MsgPort *MyPort = NULL;
  50. struct MyMessage *msg;
  51. struct node_info nody;
  52.  
  53. #define DROP if(EXIT_FLAG) CloseStuff();
  54.  
  55. void CloseStuff();
  56. int  DOORIO();
  57. void getkey(char character[]);
  58. void input(char mstring[],int len);
  59. void WriteFile(char ostring[],char mstring[]);
  60. void Send(BYTE);
  61. void ListNodes(void);
  62. int Loadnody(int node);
  63. void CheckWaitingFile(void);
  64. int CheckKey(void);
  65. void Activity(BYTE ,char *);
  66.  
  67. //******************************
  68. //*****  Global Variables  *****
  69. //******************************
  70.  
  71. int  NODE,NODES;
  72. int  EXIT_FLAG, DOOR_DATA, DOOR_CAR, DOOR_VALUE, DOOR_VALUE1 = 0,
  73.      Error,first = 0;
  74. char MyName[255],st[100],DOOR_MSG[255],DOOR_MSG1[255],nodename[255];
  75.  
  76. char Lines[5][100];
  77.  
  78. //*****************************
  79. //*****  PL (Print Line)  *****
  80. //*****************************
  81.  
  82. void pl(fmt,a1,a2,a3,a4)
  83. char *fmt;
  84.  {
  85.   char s[255];
  86.   sprintf(s,fmt,a1,a2,a3,a4);
  87.   DOOR_DATA=1; strcpy(DOOR_MSG,s); DOORIO();
  88.  }
  89.  
  90. //******************
  91. //*****  Main  *****
  92. //******************
  93.  
  94. void main(int argc,char *argv[])
  95. {
  96.  char key,string[255],string1[255],Input[150],ch;
  97.  int stat,file,sending=0,line=0,first;
  98.  register x,y;
  99.  
  100.  if(!DoorStart(argv[1])) { PutStr("[os2] Tempest Door\n"); exit(0); }
  101.  
  102.  pl("\f[Internode Line Chat Module v0.6]   By Michael Bockety [01-27-94]\r\n");
  103.  pl("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  104.  
  105. //********************************
  106. //*****  Load Internal.Data  *****
  107. //********************************
  108.  
  109.  NODE = atoi(argv[1]);
  110.  stat=Loadnody(NODE);
  111.  if(stat==0)
  112.   {
  113.    pl("NodeInfo File Not Found!\r\n");
  114.    CloseStuff();
  115.   }
  116.  NODES = nody.SystemDataInfo->NumberOfLines + 1;
  117.  
  118.  Activity(33,"");
  119.  
  120.  ListNodes();
  121.  
  122.  sprintf(string,"NODE:CBText-%d",NODE);
  123.  DeleteFile(string);
  124.  
  125. //*********************************
  126. //*****  Get Input & Send it  *****
  127. //*********************************
  128.  
  129.  strcpy(Lines[0],"Entered the chat system!\r\n");
  130.  Send(0);
  131.  
  132.  REDO:
  133.  
  134.  pl("\r\n\r\n<CTRL-PPage node for Chat\r\n<CTRL-SSend Private Msg to node\r\n<CTRL-WWho's Online.\r\n<CTRL-XExit Chat.\r\n\r\n");
  135.  
  136.  PROMPT:
  137.  
  138.  x=0;
  139.  line=0;
  140.  
  141.  setmem(&Lines[0],sizeof(Lines[0]),NULL);
  142.  while(1)
  143.   {
  144.    REINPUT:
  145.    line=0;
  146.    first=0;
  147.    CheckWaitingFile();
  148.    key=CheckKey();
  149.    DROP;
  150.    if(key!=0)
  151.     {
  152.      while(1)
  153.       {
  154.        if(first!=0)
  155.         {
  156.          getkey(Input);
  157.          DROP;
  158.         }
  159.        else Input[0]=key;
  160.        first=1;
  161.        key=NULL;
  162.  
  163.        if((sending==0)&&(Input[0]!=13))
  164.         {
  165.          pl("<%s>\r\n1>",User.Name);
  166.          sending=1;
  167.          x=0;
  168.         }
  169.        switch(Input[0])
  170.         {
  171.          case 13: RETURNKEY: // Return Key
  172.                   if(x<1)
  173.                    {
  174.                     line=0;
  175.                     pl("\r\n");
  176.                     goto PROMPT;
  177.                    }    // Return without input
  178.                   Lines[line][x]=NULL;
  179.                   x=0;
  180.                   if(Lines[0][0]!=NULL) pl("\r\nSending Text\r\n\r\n");
  181.                   line=0;
  182.                   Send(0);
  183.                   sending=0;
  184.                   goto PROMPT;
  185.  
  186.          case 8:  // BackSpace
  187.                   if(x<1) goto REINPUT;
  188.                   pl("\b \b");
  189.                   x--;
  190.                   Lines[line][x]=NULL;
  191.                   if(x==0)
  192.                    {
  193. //                    sending=0;
  194.                     goto PROMPT;
  195.                    }
  196.                   break;
  197.  
  198.          case 24: // CTRL-X = Quit Program
  199.                   if(Lines[0][0]!=NULL) pl("\r\nSending Text\r\n\r\n");
  200.                   pl("\r\n\r\n");
  201.                   Send(0);
  202.                   strcpy(Lines[0],"Exiting the chat system!\r\n");
  203.                   Send(0);
  204.                   Activity(30,"");
  205.                   sprintf(string,"NODE:CBText-%d",NODE);
  206.                   DeleteFile(string);
  207.                   CloseStuff();
  208.  
  209.          case 23: // CTRL-W = List Nodes
  210.                   Send(0);
  211.                   sending=0;
  212.                   ListNodes();
  213.                   goto REDO;
  214.  
  215.  
  216.          case 19: // CTRL-S = Send Invite
  217.          case 16: // CTRL-P = Page Node
  218.                   Send(0);
  219.                   sending=0;
  220.                   ListNodes();
  221.                   pl("\r\n\r\nSend to which node number");
  222.                   getkey(string);
  223.                   DROP;
  224.                   y = atoi(string);
  225.                   stat=Loadnody(y);
  226.                   if((nody.UserInfo->Name[0]!=NULL)&&(stat!=0))
  227.                    {
  228.                     if((*nody.AN!=58)&&(*nody.AN!=56)&&(*nody.AN!=4))
  229.                      {
  230.                       if(Input[0]==16)
  231.                        {
  232.                         sprintf(string,"NODE:MsgToNode-%d",y);
  233.                         sprintf(string1,"\r\n(%swould like to chat in InterNode Chat\r\n",User.Name);
  234.                         WriteFile(string,string1);
  235.                        }
  236.                       else
  237.                        {
  238.                         pl("\r\n\r\nInput your message (1 Line)\r\n");
  239.                         pl(">");
  240.                         input(Lines[0],77);
  241.                         DROP;
  242.                         if(Lines[0][0]!=NULL) pl("Sending Text\r\n\r\n");
  243.                         Send(y);
  244.                        }
  245.                      }
  246.                     else pl("\r\nYou can not chat that user at this time!\r\n\r\n");
  247.                     goto REDO;
  248.                    }
  249.                   else pl("\r\nNobody on that node!\r\n\r\n");
  250.                   goto PROMPT;
  251.  
  252.          default: ch = Input[0];
  253.                   Lines[line][x] = ch;
  254.                   sprintf(string,"%c",ch);
  255.                   pl(string);
  256.                   x++;
  257.                   if(x>=77)
  258.                    {
  259.                     line++;
  260.                     if(line>=5) goto RETURNKEY;
  261.                     x=0;
  262.                     pl("\r\n%d>",(line+1));
  263.                    }
  264.         }
  265.       }
  266.     }
  267.   }
  268. }
  269.  
  270. //************************************
  271. //*****  Send Text To All Nodes  *****
  272. //************************************
  273.  
  274. void Send(BYTE TYPE)
  275. {
  276.  int x,y=1,stat;
  277.  char OutFile[50],SendString[700];
  278.  if(strlen(Lines[0])==0) return;
  279.  
  280.  sprintf(SendString,"\nInternode Message From (%s)\n",User.Name);
  281.  
  282.  for(x=0;x<5;x++)
  283.   {
  284.    if(Lines[x][0]!=NULL) strcat(SendString,Lines[x]);
  285.    setmem(&Lines[x],sizeof(Lines[x]),NULL);
  286.   }
  287.  if(TYPE>1)
  288.   {
  289.    stat=Loadnody(TYPE);
  290.    if(stat==0) return;
  291.    if((*nody.AN==33)&&(TYPE!=NODE))
  292.     {
  293.      sprintf(OutFile,"NODE:CBText-%d",TYPE);
  294.      WriteFile(OutFile,SendString);
  295.     }
  296.   }
  297.  else
  298.   {
  299.    for(y=1;y<NODES+1;y++)
  300.     {
  301.      stat=Loadnody(y);
  302.      if(stat!=0)
  303.       {
  304.        if((*nody.AN==33)&&(y!=NODE))
  305.         {
  306.          sprintf(OutFile,"NODE:CBText-%d",y);
  307.          WriteFile(OutFile,SendString);
  308.         }
  309.       }
  310.     }
  311.   }
  312.  CheckWaitingFile();
  313. }
  314.  
  315. //************************************
  316. //*****  Check for Waiting File  *****
  317. //************************************
  318.  
  319. void CheckWaitingFile(void)
  320. {
  321.  char string[200];
  322.  int stat;
  323.  FILE *in;
  324.  
  325.  sprintf(string,"NODE:CBText-%d",NODE);
  326.  
  327.  in=fopen(string,"r");
  328.  if(in==NULL) return;
  329.  pl("\r\n");
  330.  while(fgets(string,100,in)!=NULL)
  331.   {
  332.    pl("%s\r",string);
  333.   }
  334.  fclose(in);
  335.  sprintf(string,"NODE:CBText-%d",NODE);
  336.  DeleteFile(string);
  337.  pl("\r\n");
  338. }
  339.  
  340. //************************
  341. //*****  List Nodes  *****
  342. //************************
  343.  
  344. void ListNodes(void)
  345. {
  346.  char string[200],string2[20];
  347.  char Handle[40],Activity[30],Time[15];
  348.  register int i;
  349.  int stat;
  350.  
  351.  pl("\r\n\r\n     .----------------------------------------------------------------.\r\n");
  352.  pl("     NODE LoGoN HANDLE                    ACTiViTY            |\r\n");
  353.  
  354. //***************************
  355. //*****  Who is Online  *****
  356. //***************************
  357.  
  358.  i=1;
  359.  do
  360.   {
  361.    stat=Loadnody(i);
  362.    if(stat==0) setmem(&nody,sizeof(struct node_info),NULL);
  363.    strcpy(Handle,nody.UserInfo->Name);
  364.    if((*nody.hide==1)&&(User.Security!=255)&&(User.Name[0]!=NULL))
  365.     {
  366.      if(nody.UserInfo->Slot_Number==1) Handle[0]=NULL;
  367.      else strcpy(Handle,"[ Hiding From Display ]");
  368.     }
  369.    if(Handle[0]!=NULL)
  370.     {
  371.      sprintf(string,"%s",ctime(&nody.UserInfo->Time_Last));
  372.      strmid(string,Time,12,5);
  373.      strcpy(Activity,nody.activity);
  374.      //**************************
  375.      //*****  Final Output  *****
  376.      //**************************
  377.      pl("     |------|-------|---------------------------|---------------------|\r\n");
  378. //     if(nody.NodeDataInfo->LocalMode==0)
  379.      sprintf(string2,"%2d",i);
  380. //     else                                strcpy(string2," V");
  381.      pl("     |  %2s  %5s %-25.25s %-19s |\r\n",
  382.          string2,Time,Handle,Activity);
  383.     }
  384.    i++;
  385.   }
  386.  while(i<NODES);
  387.  pl("     `----------------------------------------------------------------'");
  388. }
  389.  
  390. //************************
  391. //*****  Load Nody  ******
  392. //************************
  393.  
  394. int Loadnody(int node)
  395. {
  396.  char string[50];
  397.  register int y;
  398.  int file;
  399.  sprintf(string,"Node:NodeInfo_%d",node);
  400.  for(y=1;y<3;y++)
  401.   {
  402.    file=Open(string,MODE_OLDFILE);
  403.    if(file!=0)
  404.     {
  405.      Read(file,(char *)&nody,sizeof(struct node_info));
  406.      Close(file);
  407.      return(1);
  408.     }
  409.   }
  410.  return(0);
  411. }
  412.  
  413. //*************************
  414. //*****  Close Stuff  *****
  415. //*************************
  416.  
  417. void CloseStuff()
  418. { DOOR_DATA=99; strcpy(DOOR_MSG,'\0'); DOORIO();
  419.  while(msg=(struct MyMessage *)GetMsg(MyPort)) ReplyMsg((struct Message *)msg);
  420.  if(MyPort) DeletePort(MyPort);
  421.  exit(0); }
  422.  
  423. //********************
  424. //*****  DoorIO  *****
  425. //********************
  426.  
  427. int DOORIO()
  428. {
  429.  struct MsgPort *HisPort;
  430.  struct MyMessage message;
  431.  int UPDATE;
  432.  if(EXIT_FLAG) return 0;
  433.  UPDATE=0;
  434.  HisPort = FindPort(st);
  435.  if(HisPort!=NULL)
  436.   {
  437.    message.Msg.mn_Node.ln_Type = NT_MESSAGE;
  438.    message.Msg.mn_Length = sizeof(message);
  439.    message.Msg.mn_ReplyPort = MyPort;
  440.    if(first)CopyMem(&User,&message.User,sizeof(struct User));
  441.    message.car   = 0;
  442.    message.Data  = DOOR_DATA;
  443.    message.Value = DOOR_VALUE;
  444.    message.IntValue = DOOR_VALUE1;
  445.    strcpy(message.text,DOOR_MSG);
  446.    strcpy(message.text1,DOOR_MSG1);
  447.    PutMsg((struct MsgPort *)HisPort,(struct Message *)&message);
  448.    Wait(1 << MyPort->mp_SigBit);
  449.    GetMsg(MyPort);
  450.    DOOR_DATA  = message.Data;
  451.    DOOR_VALUE = message.Value;
  452.    DOOR_VALUE1 = message.IntValue;
  453.    strcpy(DOOR_MSG,message.text);
  454.    strcpy(DOOR_MSG1,message.text1);
  455.    CopyMem(&message.User,&User,sizeof(struct User));
  456.    first=1;
  457.    UPDATE=0;
  458.    if(message.car) EXIT_FLAG=1;
  459.   }
  460.  return 0;
  461. }
  462.  
  463. //************************
  464. //*****  Door Start  *****
  465. //************************
  466.  
  467. DoorStart(char node[])
  468. {
  469.  struct MsgPort *HisPort;
  470.  EXIT_FLAG = 0;
  471.  sprintf(st,"%s:TEMPEST_DOOR",node);
  472.  HisPort = FindPort(st);
  473.  if(HisPort==NULL) return (FALSE);
  474.  sprintf(MyName,"%s:DOOR_PORT",node);
  475.  MyPort = CreatePort(MyName,0L);
  476.  if(MyPort==NULL)
  477.   {
  478.    PutStr("Cant open port");
  479.    return(int)FALSE;
  480.   }
  481.  DOORIO();
  482.  return (int)TRUE;
  483. }
  484.  
  485. //*********************
  486. //*****  Get Key  *****
  487. //*********************
  488.  
  489. void getkey(char character[])
  490. { strcpy(DOOR_MSG,'\0'); DOOR_DATA=31; DOORIO(); strcpy(character,DOOR_MSG); }
  491.  
  492. //*******************
  493. //*****  Input  *****
  494. //*******************
  495.  
  496. void input(char mstring[],int len)
  497. { DOOR_DATA=40; DOOR_VALUE=len; strcpy(DOOR_MSG,mstring); DOORIO();
  498.  strcpy(mstring,DOOR_MSG); strcpy(DOOR_MSG,'\0'); strcpy(DOOR_MSG1,'\0'); }
  499.  
  500. //************************
  501. //*****  Write File  *****
  502. //************************
  503.  
  504. void WriteFile(char ostring[],char mstring[])
  505. { DOOR_DATA=16; strcpy(DOOR_MSG,ostring); strcpy(DOOR_MSG1,mstring); DOORIO();
  506.  strcpy(DOOR_MSG,'\0'); strcpy(DOOR_MSG1,'\0'); }
  507.  
  508.  
  509.  //***********************
  510.  //*****  Check Key  *****
  511.  //***********************
  512.  
  513.  // This will not wait for a key to be hit, it just simply tells me if
  514.  // there has been one hit, if so the character will be in integer form
  515.  // returned via (x) in this example
  516.  
  517.  int CheckKey(void)
  518.   { strcpy(DOOR_MSG,'\0'); DOOR_DATA=54; DOOR_VALUE1=0; DOORIO();
  519.     return(DOOR_VALUE1); }
  520.  
  521.  //**********************
  522.  //*****  Activity  *****
  523.  //**********************
  524.  
  525.  void Activity(BYTE act,char *text)
  526.  { DOOR_DATA=72; DOOR_VALUE=act; strcpy(DOOR_MSG,text); DOORIO(); }
  527.